package com.example.service.impl;

import com.example.dao.UserDao;
import com.example.dao.impl.UserDaoImpl;
import com.example.model.User;
import com.example.service.UserService;

import java.util.List;

/**
 * 用户服务实现类
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    
    @Override
    public boolean register(User user) {
        // 先检查用户名是否已存在
        User existUser = userDao.findByUsername(user.getUsername());
        if (existUser != null) {
            return false; // 用户名已存在
        }
        // 执行注册
        int result = userDao.add(user);
        return result > 0;
    }

    @Override
    public User login(String username, String password) {
        return userDao.login(username, password);
    }

    @Override
    public boolean deleteUser(int id) {
        int result = userDao.delete(id);
        return result > 0;
    }

    @Override
    public boolean updateUser(User user) {
        int result = userDao.update(user);
        return result > 0;
    }

    @Override
    public User getUserById(int id) {
        return userDao.findById(id);
    }

    @Override
    public User getUserByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    public List<User> getAllUsers() {
        return userDao.findAll();
    }
} 